package com.yuven.baselib.component;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.CallSuper;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.DialogFragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.yuven.baselib.R;
import com.yuven.baselib.component.bean.DialogBean;
import com.yuven.baselib.component.viewmodel.BaseViewModel;
import com.yuven.baselib.widget.DefaultProgressSpinner;
import com.yuven.baselib.widget.ProgressSpinner;
import com.hjq.toast.ToastUtils;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author :        Yuven
 * @date :          2022/7/6 7:00 下午
 * @desc :
 */
public abstract class BaseMVVMDialogFragment<VM extends BaseViewModel, DB extends ViewDataBinding> extends DialogFragment {
    protected VM viewModel = null;
    protected CompositeDisposable mCompositeDisposable = null;
    protected DB dataBinding = null;
    protected ProgressSpinner mProgressSpinner = null;

    protected AppCompatActivity mActivity;
    protected Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (AppCompatActivity) (mContext = context);
    }

    @CallSuper
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initIntent();
        dataBinding = initDataBinding(inflater, layoutId(), container);
        viewModel = new ViewModelProvider(this, new ViewModelProvider.Factory() {

            @NonNull
            @Override
            public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
                try {
                    return modelClass.newInstance();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (java.lang.InstantiationException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }).get(viewModelClass());
        return dataBinding.getRoot();
    }


    @CallSuper
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initObserver();
        initData();
        initView();
    }

    protected void initObserver() {
        viewModel.observeDialog(this, new Observer<DialogBean>() {
            @Override
            public void onChanged(@Nullable DialogBean dialogBean) {
                if (dialogBean.isShow()) {
                    showLoading(dialogBean.getMsg(), dialogBean.isCancelable());
                } else {
                    hideLoading();
                }
            }
        });
        viewModel.observeToast(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                ToastUtils.show(s);
            }
        });
    }

    /**
     * 创建默认实现的
     *
     * @return
     */
    protected ProgressSpinner createProgressSpinner() {
        return new DefaultProgressSpinner(mContext);
    }

    protected void showLoading(String msg) {
        showLoading(msg, true);
    }

    protected void showLoading(String msg, boolean cancelable) {
        if (mProgressSpinner == null) {
            mProgressSpinner = createProgressSpinner();
        }
        if (mProgressSpinner != null && !mProgressSpinner.isShowing()) {
            mProgressSpinner.show(msg, cancelable);
        }
    }

    protected void hideLoading() {
        if (mProgressSpinner != null && mProgressSpinner.isShowing()) {
            mProgressSpinner.dismiss();
        }
    }


    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initIntent();

    /**
     * 初始化DataBinding
     */
    protected DB initDataBinding(
            LayoutInflater inflater,
            @LayoutRes int layoutId,
            ViewGroup container
    ) {
        return DataBindingUtil.inflate(inflater, layoutId, container, false);
    }

    /**
     * 布局id
     */
    protected abstract int layoutId();

    /**
     * VM
     */
    protected abstract Class<VM> viewModelClass();

    /**
     * 添加RxJava订阅
     */
    protected void addDisposable(Disposable mDisposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        if (mDisposable != null) {
            mCompositeDisposable.add(mDisposable);
        }
    }

    /**
     * 取消所有RxJava订阅
     */
    protected void clearDisposable() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        clearDisposable();
    }
}
